package sk.stu.fei.tomsteam;

import java.io.IOException;
import org.snmp4j.smi.OID;
import javax.swing.*;
import javax.swing.border.EtchedBorder;

import java.awt.BorderLayout;
import java.awt.Dimension;

import javax.swing.BoxLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

public class Main {

	private static final long serialVersionUID = 1L;
	TreeInPanel panelWest;	//tree
	AgentsMonitoringPanel poolingPanel;	//displayed SNMP monitored agents info
	JMenuBar menubar;
	JFrame uiFrame;
	Container contentPane;
	JTextArea trapJTextArea;
	JScrollPane trapPanel;
	JPanel panelCenter;

	public Main() {
		
		//setLayout(new BorderLayout());	contentPanes use BorderLayout by default
		uiFrame = new JFrame();
		contentPane = uiFrame.getContentPane();
		
		trapJTextArea = new JTextArea();
		trapPanel = new JScrollPane(trapJTextArea);
		trapPanel.setPreferredSize(new Dimension(450, 140));
		panelCenter = new JPanel();
		panelCenter.setLayout(new BoxLayout(panelCenter, BoxLayout.PAGE_AXIS));
		panelCenter.add(trapPanel);
		//contentPane.add(jTextAreaSNMPAlarmOutput, BorderLayout.LINE_END);
		
		Thread snmpThread = new Thread(new TrapReceiver(trapJTextArea, "0.0.0.0", 162));
		snmpThread.start();
		
		JLabel statusbar = new JLabel("TeamProject");
		statusbar.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.RAISED));
		contentPane.add(statusbar, BorderLayout.PAGE_END);
		
		
		menubar = new JMenuBar();
		//ImageIcon icon = new ImageIcon();
		JMenu agentMenu = new JMenu("Agent");
		agentMenu.setMnemonic(KeyEvent.VK_A);	//klavesova skratka po stlaceni altu
		JMenu helpMenu = new JMenu("Help");
		helpMenu.setMnemonic(KeyEvent.VK_H);
		
		JMenuItem addAgentMenuItem = new JMenuItem("Add");
		addAgentMenuItem.setMnemonic(KeyEvent.VK_A);
		addAgentMenuItem.setToolTipText("Add new agent");
		JMenuItem aboutHelpMenuItem = new JMenuItem("About");
		aboutHelpMenuItem.setMnemonic(KeyEvent.VK_A);
		aboutHelpMenuItem.setToolTipText("About this software");
		
		addAgentMenuItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent event) {
	            //JOptionPane.showMessageDialog(getComponent(0), "Adding new agent");
				AddAgentDialog aad = new AddAgentDialog(panelWest, uiFrame);
				aad.setVisible(true);
			}
		});
		
		aboutHelpMenuItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent event) {
	            JOptionPane.showMessageDialog(uiFrame, "Ut Fei Stu 2013");
			}
		});
		
		agentMenu.add(addAgentMenuItem);
		menubar.add(agentMenu);
		helpMenu.add(aboutHelpMenuItem);
		menubar.add(helpMenu);
		uiFrame.setJMenuBar(menubar);
		
	    
		//JLabel label = new JLabel(executorMethod());
        //getContentPane().add(label);
	    	    
	    poolingPanel = new AgentsMonitoringPanel(this);	//passing mainWindow object component to other object
	    panelCenter.add(poolingPanel);
	    contentPane.add(panelCenter, BorderLayout.CENTER);
	    
	    panelWest = new TreeInPanel();
	    contentPane.add(panelWest, BorderLayout.LINE_START);
	    
	    //uiFrame.setUndecorated(true);
	    //uiFrame.setOpacity(0.8f);
	    uiFrame.setTitle("Network JWatcher");
	    uiFrame.setSize(700, 400);
	    uiFrame.setLocationRelativeTo(null);
	    uiFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	    uiFrame.setVisible(true);
	}

	 
	    public static void main(String[] args) {
	    	
	    	try {
	    		UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
	    	}
	    	catch (ClassNotFoundException | InstantiationException |
	    			IllegalAccessException | UnsupportedLookAndFeelException ex) {
	    		ex.printStackTrace();
	    	}
	    	
	    	SwingUtilities.invokeLater(new Runnable() {
	        	@Override
	            public void run() {
	                Main m = new Main();
	            }
	        });
	    }
}
	

